// JavaScript Document

var xmlhttp;
var statenow=0;
function makeXMLHTTP(){
	var myxht;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	  myxht=new XMLHttpRequest();
	}else
	{// code for IE6, IE5
	  myxht=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return myxht;
}
function loadXMLDoc(url,cfunc)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	}else
	{// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=cfunc;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}
function loadXMLDocMy(myxmlhttp,url,cfunc)
{
	myxmlhttp.onreadystatechange=cfunc;
	myxmlhttp.open("GET",url,true);
	myxmlhttp.send();
}
function loadXMLDocPOST(url,param,cfunc)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	}else
	{// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=cfunc;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(param);
}

function headerChange(){
	loadXMLDoc("php/login.php?xhttp=true",function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{	var respon=xmlhttp.responseText ;
			if (respon != statenow){
				if (respon==0) document.location = "home.php";
				else
				loadXMLDoc("php/viewer/header.php",function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)
					{
						document.getElementById("logprofpicarea").innerHTML=xmlhttp.responseText;
					}
				});
			}
			statenow = respon;
		}
	});
}

function divUpload(){
	loadXMLDoc("php/viewer/photo_upload.php",function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("contento").innerHTML=xmlhttp.responseText;
		}
	});
}

function divPhotoList(uname){
	loadXMLDoc("php/viewer/photo_list.php?un="+uname,function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("contento").innerHTML=xmlhttp.responseText;
		}
	});
}

function photoDetail(oper,uname,idphoto){
	loadXMLDoc("php/viewer/photo_detail.php?op="+oper+"&un="+uname+"&id="+idphoto,function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("contento").innerHTML=xmlhttp.responseText;
		}
	});
}
function photoDetailArea(oper,uname,idphoto){
	loadXMLDoc("php/viewer/photo_detail.php?op="+oper+"&un="+uname+"&id="+idphoto,function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("detarea").innerHTML=xmlhttp.responseText;
		}
	});
}

function morePhoto(uname,last){
	loadXMLDoc("php/viewer/photo_listn.php?un="+uname+"&last="+last,function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("photoarea").innerHTML+=xmlhttp.responseText;
			document.getElementById("morePhoto").onclick= function (event){morePhoto(uname,last+5);};
			
			if (document.getElementById("hbs").value==1){
				document.getElementById("morePhoto").style.visibility = "hidden";
			}
		}
	});
}

function getComments(uname,idfoto,last){
	loadXMLDoc("php/viewer/photo_comments.php?un="+uname+"&id="+idfoto+"&last="+last,function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("botpanelcomments").innerHTML=xmlhttp.responseText;
		}
	});
}



function getOnePost(idpost){
	loadXMLDoc("php/viewer/wall_onepost.php?op=onepost&id="+idpost,function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("post"+idpost).innerHTML=xmlhttp.responseText;
		}
	});
}
function getNPost(uname,from,lgth){
	loadXMLDoc("php/viewer/wall_onepost.php?op=npost&un="+uname+"&fr="+from+"&ln="+lgth,function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('listpost').innerHTML+=xmlhttp.responseText;
			document.getElementById("morePost").onclick= function (event){getNPost(uname,from+3,lgth);};
			if (document.getElementById("posthbs").value==1){
				document.getElementById("morePost").style.visibility = "hidden";
			}
		}
	});
}

function getNPostComment(idpost,from,lgth){
	loadXMLDoc("php/viewer/wall_onepost.php?op=ncomment&id="+idpost+"&fr="+from+"&ln="+lgth,function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("listpostcomm"+idpost).innerHTML+=xmlhttp.responseText;
			document.getElementById("morePostComm"+idpost).onclick= function (event){getNPostComment(idpost,from+3,lgth);};
			
			if (document.getElementById("postcmthbs"+idpost).value==1){
				document.getElementById("morePostComm"+idpost).style.visibility = "hidden";
			}
		}
	});
}

//=================================AJAX FORM==============================================
/** Mengubah value di form jadi bentuk query name=value&name=value*/
function serialize(form) {
  if (!form || !form.elements) return;

  var serial = [], i, j, first;
  var add = function (name, value) {
    serial.push(encodeURIComponent(name) + '=' + encodeURIComponent(value));
  }

  var elems = form.elements;
  for (i = 0; i < elems.length; i += 1, first = false) {
    if (elems[i].name.length > 0) { /* don't include unnamed elements */
      switch (elems[i].type) {
        case 'select-one': first = true;
        case 'select-multiple':
          for (j = 0; j < elems[i].options.length; j += 1)
            if (elems[i].options[j].selected) {
              add(elems[i].name, elems[i].options[j].value);
              if (first) break; /* stop searching for select-one */
            }
          break;
        case 'checkbox':
        case 'radio': if (!elems[i].checked) break; /* else continue */
        default: add(elems[i].name, elems[i].value); break;
      }
    }
  }

  return serial.join('&');
}

function sendForm(form,cfunc){
	var query = serialize(form);
	loadXMLDocPOST(form.action,query,cfunc);
	//alert('sending');
}

function setAvatar(uname,idfoto){
	sendForm(document.forms['avt'],function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{	
			document.getElementById("logprofpicarea").innerHTML = xmlhttp.responseText;
			getComments(uname,idfoto,0);
		}
	});
}

function editPhoto(loguname,idfoto){
	sendForm(document.forms['editphoto'],function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			photoDetailArea('detail',loguname,idfoto);
		}
	});	
}
function delPhoto(uname){
	sendForm(document.forms['delphoto'],function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			divPhotoList(uname);
		}
	});
}
function addComment(uname,idfoto){
		sendForm(document.forms['addcomment'],function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			getComments(uname,idfoto,-1);
			hide('commentinput');$(this.id).value='';
		}
		});
}

function delComment(uname,idfoto,idkom,fpage){
		sendForm(document.forms['delcomment'+idkom],function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			getComments(uname,idfoto,fpage);
		}
		});
}

function doTag(loguname,idfoto){
	//document.forms['tagphoto'].submit();
	sendForm(document.forms['tagphoto'],function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			photoDetailArea('detail',loguname,idfoto);
		}
	});
}

function addPost(uname){
	sendForm(document.forms["postingan"],function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById('postingan').value='';
			document.getElementById('listpost').innerHTML='';
			getNPost(uname,0,3);
		}
	});
}


function doLike(uname,idfoto){
	sendForm(document.forms['like'],function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			photoDetailArea('detail',uname,idfoto);
		}
	});
}

function doDislike(uname,idfoto){
	sendForm(document.forms['dislike'],function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			photoDetailArea('detail',uname,idfoto);
		}
	});
}

function addCommentPost(form,idpost,uname){
	sendForm(form,function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			getOnePost(idpost);
			//document.getElementById(postnya).innerHTML = xmlhttp.responseText;
		}
	});	
}

///////////////////////////////////////////////// NOTIF
function getAllNotif(){
	document.getElementById('notifbutton').style.fontWeight='normal';
	loadXMLDoc("php/userop.php?op=getnotif&ls=0",function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{	
			document.getElementById("listnotif").innerHTML=xmlhttp.responseText;
			document.getElementById("morenotif").onclick= function (event){getNotif(3);};
			if (document.getElementById("notifhbs").value==1){
				document.getElementById("morenotif").style.visibility = "hidden";
			}
		}
	});
}
function getNotif(last){
	var myxmlhttp = makeXMLHTTP();
	loadXMLDocMy(myxmlhttp,"php/userop.php?op=getnotif&ls="+last,function(){
		if (myxmlhttp.readyState==4 && myxmlhttp.status==200)
		{
			document.getElementById("listnotif").innerHTML+=myxmlhttp.responseText;
			document.getElementById("morenotif").onclick= function (event){getNotif(3);};
			
			if (document.getElementById("notifhbs").value==1){
				document.getElementById("morenotif").style.visibility = "hidden";
			}
		}
	});
}

var NNotif=0;
function NotifCheck(){
	var nown=0;
	var myxmlhttp = makeXMLHTTP();
	loadXMLDocMy(myxmlhttp,"php/userop.php?op=getnnotif",function(){
		if (myxmlhttp.readyState==4 && myxmlhttp.status==200){
			nown=myxmlhttp.responseText;	
			if (nown!=NNotif){
				NNotif = nown;
				document.getElementById("notifbutton").innerHTML='<a style="color:#CCC" href="javascript:void()" onclick="getAllNotif();show(\'notification\');">You have '+NNotif+' new notification(s).</a>';
				document.getElementById('notifbutton').style.fontWeight='bold';
			}
		}
	});
	
}
